<?php

declare(strict_types=1);

namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Service\UserService;
use App\Service\CommService;
use App\Service\PlayletService;
use App\Service\RechargeService;
use Monolog\Handler\IFTTTHandler;

#[AutoController]
class PlayletController extends BaseController
{
    #[Inject]
    private UserService $userService;
    #[Inject]
    private CommService $commService;
    #[Inject]
    private PlayletService $playletService;
    #[Inject]
    private RechargeService $rechargeService;

    public function index(RequestInterface $request, ResponseInterface $response)
    {
        return $response->raw('ok');
    }

    #观看记录
    public function view_records(RequestInterface $request,ResponseInterface $response){
        $xc_open_id = $request->input("xc_open_id","");
        $playlet_id = $request->input("playlet_id","");
        $playlet_number = $request->input("playlet_number","1");
        $applet_id = $this->get_applet_id();
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = !empty($open_id_info['id']) ? $open_id_info['id'] : "";
        if (empty($user_id) || $playlet_id == ""){
            return $this->resultResponse(["code"=>20001]);
        }
        $rec_data = $this->playletService->view_records_add($applet_id,$user_id,$playlet_id,$playlet_number);
        return $this->resultResponse(["data"=>[] ]);
    }
    public function recommend(RequestInterface $request,ResponseInterface $response){
        $xc_open_id = $request->input("xc_open_id","");
        $applet_id = $this->get_applet_id();
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = !empty($open_id_info['id']) ? $open_id_info['id'] : "";
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $rec_data = $this->playletService->get_user_recommend($applet_id,$user_id);
        var_dump($rec_data);
        return $this->resultResponse(["data"=>$rec_data]);
    }

    public function zl_list(RequestInterface $request,ResponseInterface $response){
        #推荐点赞和追剧列表
        $xc_open_id = $request->input("xc_open_id","");
        $applet_id = $this->get_applet_id();
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = !empty($open_id_info['id']) ? $open_id_info['id'] : "";
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $rec_data = $this->playletService->get_user_recommend_zl_list($applet_id,$user_id);
        return $this->resultResponse(["data"=>$rec_data]);
    }

    public function episodes_info(RequestInterface $request,ResponseInterface $response){
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input("xc_open_id","");
        $playlet_id = $request->input("playlet_id","");
        $playlet_number = $request->input("playlet_number","1");
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = !empty($open_id_info['id']) ? $open_id_info['id'] : "";
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $rec_data = $this->playletService->get_episodes_info($user_id,$applet_id,$playlet_id,$playlet_number);
        if ($rec_data["code"] == 10000){
            return $this->resultResponse(["data"=>$rec_data]);
        }else{
            return $this->resultResponse(["code"=>$rec_data["code"]]);
        }

    }
    #获取短剧列表.包括收费章节
    public function episodes_list(RequestInterface $request,ResponseInterface $response){
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input("xc_open_id","");
        $playlet_id = $request->input("playlet_id","");
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = !empty($open_id_info['id']) ? $open_id_info['id'] : "";
        $spread_id = !empty($open_id_info['spread_id']) ? $open_id_info['spread_id'] : "";

        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $rec_data = $this->playletService->get_my_episodes_list($applet_id,$playlet_id,$user_id,$spread_id);


        return $this->resultResponse(["data"=>$rec_data]);
    }

    public function buy(RequestInterface $request,ResponseInterface $response){
        # 购买短剧
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input("xc_open_id","");
        $playlet_id = $request->input("playlet_id","");
        $playlet_number = $request->input("playlet_number","");
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = !empty($open_id_info['id']) ? $open_id_info['id'] : "";
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        if (empty($playlet_id) or empty($playlet_number)){
            return $this->resultResponse(["code"=>20060]);
        }
        var_dump('购买短剧');
        $list_data = $this->rechargeService->buy_playlet($applet_id,$user_id,$playlet_id,$playlet_number);
        return $this->resultResponse(["code"=>$list_data["code"]]);
    }

    public function buy_episodes_list(RequestInterface $request,ResponseInterface $response){
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input("xc_open_id","");
        $playlet_id = $request->input("playlet_id","");
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = !empty($open_id_info['id']) ? $open_id_info['id'] : "";
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $list_data = $this->playletService->get_my_playlet_episodes($applet_id,$user_id,$playlet_id);
        return $this->resultResponse(["data"=>$list_data]);
    }

    public function share(RequestInterface $request,ResponseInterface $response){
        # 分享
    }

    public function likes(RequestInterface $request,ResponseInterface $response){
        # 点赞
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input("xc_open_id","");
        $playlet_id = $request->input("playlet_id","");
        $playlet_number = $request->input("playlet_number","");
        $stype  = $request->input("stype","1"); # 0 删除 1 点赞
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = !empty($open_id_info['id']) ? $open_id_info['id'] : "";
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        if (empty($playlet_id) or empty($playlet_number)){
            return $this->resultResponse(["code"=>20060]);
        }
        if ($stype == 1){
            $this->playletService->add_likes($applet_id,$user_id,$playlet_id,$playlet_number);
        }else{
            $this->playletService->del_likes($applet_id,$user_id,$playlet_id,$playlet_number);
        }
        return $this->resultResponse(["code"=>10000]);
    }

    public function sync_likes(RequestInterface $request,ResponseInterface $response){
        # 点赞
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input("xc_open_id","");
        $playlet_id = $request->input("playlet_id","");
        $playlet_numbers = $request->input("playlet_numbers","");
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = !empty($open_id_info['id']) ? $open_id_info['id'] : "";
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }

        if (empty($playlet_id)){
            return $this->resultResponse(["code"=>20060]);
        }
        $this->playletService->sync_likes($applet_id,$user_id,$playlet_id,$playlet_numbers);
        return $this->resultResponse(["code"=>10000]);
    }

    public function likes_list(RequestInterface $request,ResponseInterface $response){
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input("xc_open_id","");
        $playlet_id = $request->input("playlet_id","");
        $playlet_number = $request->input("playlet_number","");
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = !empty($open_id_info['id']) ? $open_id_info['id'] : "";
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        if (empty($playlet_id)){
            return $this->resultResponse(["code"=>20060]);
        }
        $likes = $this->playletService->get_user_likes($applet_id,$user_id,$playlet_id);
        return $this->resultResponse(["data"=>$likes]);
    }


    public function sync_chasing(RequestInterface $request,ResponseInterface $response){
        echo "同步追剧".PHP_EOL;
        # 同步追剧
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input("xc_open_id","");
        $playlet_ids = $request->input("playlet_ids","");
        print_r($xc_open_id).PHP_EOL;
        print_r($playlet_ids).PHP_EOL;
        print_r(json_decode($playlet_ids,true));
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = !empty($open_id_info['id']) ? $open_id_info['id'] : "";
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }

        $this->playletService->sync_chasing($applet_id,$user_id,$playlet_ids);
        return $this->resultResponse(["code"=>10000]);
    }

    public function chasing_list(RequestInterface $request,ResponseInterface $response){
        # 追剧列表
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input("xc_open_id","");
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = !empty($open_id_info['id']) ? $open_id_info['id'] : "";
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }

        $data = $this->playletService->get_user_chasing_list($applet_id,$user_id);
        return $this->resultResponse(["data"=>$data]);
    }

    public function sync_zl_list(RequestInterface $request,ResponseInterface $response){
        # 同步推荐数据
        echo "同步推荐数据".PHP_EOL;
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input("xc_open_id","");
        $sync_data = $request->input("sync_data","");
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = !empty($open_id_info['id']) ? $open_id_info['id'] : "";
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        if (empty($sync_data)){
            return $this->resultResponse(["code"=>20060]);
        }
        $sync_data = json_decode($sync_data,true);
        print_r($sync_data).PHP_EOL;
        $data = $this->playletService->sync_zl_list($applet_id,$user_id,$sync_data);
        return $this->resultResponse(["data"=>$data]);
    }



}
